package com.tony.tools.日期时间;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: StringDateToString
 * @Auther: 皮蛋布丁
 * @Date: 2021/11/25/13:44
 * @Description: 字符串类型时间转换
 */
public class StringDateToString {

    /**
    * @Description: AddLineToTimeDay yyyyMMdd类型转换为yyyy-MM-dd
    * @Param: [date]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    * @Date: 2021/11/25 13:47
    */
    public String AddLineToTimeDay(String date) {
        StringBuilder stringBuilder = new StringBuilder(date);
        stringBuilder.insert(4,"-");
        stringBuilder.insert(7,"-");
        date = stringBuilder.toString();
        if (isLegalTimeDay(date)) {
            return date;
        }
        return date;
    }
    /**
    * @Description: isLegalTimeDay 校验格式是否为yyyy-MM-dd
    * @Param: [sDate]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/11/25 13:48
    */
    private static boolean isLegalTimeDay(String sDate) {
        if (sDate.length() != 10) {
            return false;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(sDate);
            return sDate.equals(format.format(date));
        } catch (ParseException e) {
            return false;
        }
    }

}
